/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.agreement.web.yearcarsalepurchaseagreement;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.agreement.dao.yearcarsalepurchaseagreement.YearCarSalePurchaseAgreementDao;
import com.jeesite.modules.agreement.entity.yearcarsalepurchaseagreement.YearCarSalePurchaseAgreement;
import com.jeesite.modules.agreement.service.yearcarsalepurchaseagreement.YearCarSalePurchaseAgreementService;
import com.jeesite.modules.base.entity.guarantor.Guarantor;
import com.jeesite.modules.base.entity.supplier.Supplier;
import com.jeesite.modules.base.service.guarantor.GuarantorService;
import com.jeesite.modules.base.service.supplier.SupplierService;
import com.jeesite.modules.sys.entity.Company;
import com.jeesite.modules.sys.entity.User;
import com.jeesite.modules.sys.service.CompanyService;
import com.jeesite.modules.sys.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 年度汽车买卖协议Controller
 * @author Kilo
 * @version 2021-03-04
 */
@Controller
@RequestMapping(value = "${adminPath}/agreement/yearcarsalepurchaseagreement/yearCarSalePurchaseAgreement")
public class YearCarSalePurchaseAgreementController extends BaseController {

	@Autowired
	private YearCarSalePurchaseAgreementService yearCarSalePurchaseAgreementService;
	@Autowired
	private YearCarSalePurchaseAgreementDao yearCarSalePurchaseAgreementDao;
	@Autowired
	private GuarantorService guarantorService;

	@Autowired
	private CompanyService companyService;

	@Autowired
	private SupplierService supplierService;
	@Autowired
	private UserService userService;
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public YearCarSalePurchaseAgreement get(String yearCarSalePurchaseAgreementId, boolean isNewRecord) {
		return yearCarSalePurchaseAgreementService.get(yearCarSalePurchaseAgreementId, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("agreement:yearCarSalePurchaseAgreement:view")
	@RequestMapping(value = {"list", ""})
	public String list(YearCarSalePurchaseAgreement yearCarSalePurchaseAgreement, Model model) {
		model.addAttribute("yearCarSalePurchaseAgreement", yearCarSalePurchaseAgreement);
		List<Guarantor> guarantorList = guarantorService.findList(new Guarantor());
		model.addAttribute("guarantorList", guarantorList);
		Supplier supplier = new Supplier();
		supplier.setSupplierType("1");
		List<Supplier> supplierList = supplierService.findList(supplier);
		model.addAttribute("supplierList", supplierList);
		return "modules/agreement/yearcarsalepurchaseagreement/yearCarSalePurchaseAgreementList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("agreement:yearCarSalePurchaseAgreement:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<YearCarSalePurchaseAgreement> listData(YearCarSalePurchaseAgreement yearCarSalePurchaseAgreement, HttpServletRequest request, HttpServletResponse response) {
		yearCarSalePurchaseAgreement.setPage(new Page<>(request, response));
		Page<YearCarSalePurchaseAgreement> page = yearCarSalePurchaseAgreementService.findPage(yearCarSalePurchaseAgreement);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("agreement:yearCarSalePurchaseAgreement:view")
	@RequestMapping(value = "form")
	public String form(YearCarSalePurchaseAgreement yearCarSalePurchaseAgreement, Model model) {
		if(yearCarSalePurchaseAgreement.getIsNewRecord()){
			Company company = new Company();
			company.setCompanyCode("COM00000001");
			Company com = companyService.get(company);
			yearCarSalePurchaseAgreement.setCompanyAddress(com.getCompanyAddress());
			yearCarSalePurchaseAgreement.setCompanyFax(com.getFax());
			yearCarSalePurchaseAgreement.setCompanyPhone(com.getPhone());
			yearCarSalePurchaseAgreement.setCompany(com);
			yearCarSalePurchaseAgreement.setGuarantorId(com.getDefaultGuarantor().getGuarantorId());
			yearCarSalePurchaseAgreement.setGuarantorAddress(com.getDefaultGuarantor().getAddress());
			yearCarSalePurchaseAgreement.setGuarantorIdNumber(com.getDefaultGuarantor().getIdNumber());
			yearCarSalePurchaseAgreement.setGuarantorPhone(com.getDefaultGuarantor().getPhone());
		}
		model.addAttribute("yearCarSalePurchaseAgreement", yearCarSalePurchaseAgreement);
		List<Guarantor> guarantorList = guarantorService.findList(new Guarantor());
		model.addAttribute("guarantorList", guarantorList);
		Supplier supplier = new Supplier();
		supplier.setSupplierType("1");
		List<Supplier> supplierList = supplierService.findList(supplier);
		model.addAttribute("listUser", userService.findList(new User()));
		model.addAttribute("supplierList", supplierList);
		return "modules/agreement/yearcarsalepurchaseagreement/yearCarSalePurchaseAgreementForm";
	}

	/**
	 * 查看表单详情
	 */
	@RequiresPermissions("agreement:yearCarSalePurchaseAgreement:view")
	@RequestMapping(value = "detail")
	public String detail(YearCarSalePurchaseAgreement yearCarSalePurchaseAgreement, Model model) {
		model.addAttribute("yearCarSalePurchaseAgreement", yearCarSalePurchaseAgreement);
		List<Guarantor> guarantorList = guarantorService.findList(new Guarantor());
		model.addAttribute("guarantorList", guarantorList);
		Supplier supplier = new Supplier();
		supplier.setSupplierType("1");
		List<Supplier> supplierList = supplierService.findList(supplier);
		model.addAttribute("listUser", userService.findList(new User()));
		model.addAttribute("supplierList", supplierList);
		return "modules/agreement/yearcarsalepurchaseagreement/yearCarSalePurchaseAgreementDetail";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("agreement:yearCarSalePurchaseAgreement:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated YearCarSalePurchaseAgreement yearCarSalePurchaseAgreement) {
		YearCarSalePurchaseAgreement checkModel=new YearCarSalePurchaseAgreement();
		checkModel.setInvestorId(yearCarSalePurchaseAgreement.getInvestorId());
		checkModel.setCompany(yearCarSalePurchaseAgreement.getCompany());
		checkModel.setStartDate_check(yearCarSalePurchaseAgreement.getStartDate());
		checkModel.setYearCarSalePurchaseAgreementId(yearCarSalePurchaseAgreement.getYearCarSalePurchaseAgreementId());
		List<YearCarSalePurchaseAgreement> list=yearCarSalePurchaseAgreementDao.checkMore(checkModel);
		if(list.size()>0){
			return renderResult(Global.FALSE,text("已存在年度汽车买卖协议，请核实！"));
		}
		yearCarSalePurchaseAgreementService.save(yearCarSalePurchaseAgreement);
		return renderResult(Global.TRUE, text("保存年度汽车买卖协议成功！"));
	}
	
	/**
	 * 删除数据
	 */
	@RequiresPermissions("agreement:yearCarSalePurchaseAgreement:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(YearCarSalePurchaseAgreement yearCarSalePurchaseAgreement) {
		yearCarSalePurchaseAgreementService.delete(yearCarSalePurchaseAgreement);
		return renderResult(Global.TRUE, text("删除年度汽车买卖协议成功！"));
	}

	/**
	 * 获取公司相关信息
	 * @param companyId
	 */
	@RequestMapping(value = "getCompanyInfo")
	@ResponseBody
	public Company getCompanyInfo(@RequestParam("companyId") String companyId) {
		Company company = new Company();
		company.setCompanyCode(companyId);
		Company result = companyService.get(company);
		return result;
	}

	/**
	 * 获取资方相关信息
	 * @param supplierId
	 */
	@RequestMapping(value = "getSupplierInfo")
	@ResponseBody
	public Supplier getSupplierInfo(@RequestParam("supplierId") String supplierId) {
		Supplier supplier = new Supplier();
		supplier.setSupplierId(supplierId);
		Supplier result = supplierService.get(supplier);
		return result;
	}

	/**
	 * 获取担保人相关信息
	 * @param guarantorId
	 */
	@RequestMapping(value = "getGuarantorInfo")
	@ResponseBody
	public Guarantor getGuarantorInfo(@RequestParam("guarantorId") String guarantorId) {
		Guarantor guarantor = new Guarantor();
		guarantor.setGuarantorId(guarantorId);
		Guarantor result = guarantorService.get(guarantorId);
		return result;
	}
}